package com.lifesting.book.wci;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;

import org.antlr.runtime.ANTLRReaderStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Token;


public final class ShowToken {
	public static void main(String[] args) throws IOException {
		InputStreamReader stream = new InputStreamReader(ShowToken.class.getResourceAsStream("/hello.pas"));
		ANTLRReaderStream reader = new ANTLRReaderStream(stream);
		PascalLexer lexer = new PascalLexer(reader);
		CommonTokenStream token_stream = new CommonTokenStream(lexer);
		Properties token_labels = new Properties();
		token_labels.load(ShowToken.class.getResourceAsStream("/Pascal.tokens"));
		HashMap<String, String> token_literals = new HashMap<String, String>(token_labels.size());
		Enumeration<Object> enum_token = token_labels.keys();
		while (enum_token.hasMoreElements()){
			String token_id = (String) enum_token.nextElement();
			String token_int = String.valueOf(token_labels.get(token_id));
			token_literals.put(token_int, token_id);
		}
		token_stream.reset();
		token_stream.fill();
		System.out.println("--hello.pas----------------");
		int line = -1;
		for (int i = 0; i < token_stream.size(); i++){
			Token current_token = token_stream.get(i);
			if (current_token.getType() == PascalLexer.WS 
					|| current_token.getType() == PascalLexer.COMMENT 
					|| current_token.getType() == CharStream.EOF)
				continue;
			int new_line = current_token.getLine();
			if (new_line > line){
				System.out.printf("\n%4d行:", new_line);
				line = new_line;
			}
			System.out.printf("%s[%d] ", token_literals.get(String.valueOf(current_token.getType())), current_token.getCharPositionInLine());
		}
		System.out.println();
		stream.close();
	}
}
